package com.hospes.rmes.core;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;
import com.hospes.rmes.constants.ConstMain;

import java.io.*;
import java.lang.ref.WeakReference;

public class FileManager {

	public static final String CACHE_DIR = ConstMain.DATA_EXTERNAL_STORAGE_FOLDER + "/Cache/";

	public FileManager() {
		super();
		initalize();
	}

	public static boolean isMounted() {
		return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
	}

	private void initalize() {
		if (isMounted()) {
			//File root = Environment.getExternalStorageDirectory();
			File photosDir = new File(CACHE_DIR);
			photosDir.mkdirs();
		}
	}

	private String generateFilePath(String name) {
		//File root = Environment.getExternalStorageDirectory();
		// return new File(root, String.format("%s%s", CACHE_DIR,
		// name)).toString();
		return new File(CACHE_DIR, name).toString();
	}

	public void storeToCache(Bitmap image, ImageDescriptor imageDescriptor) {
		if (image == null) {
			return;
		}
		String path = generateFilePath(imageDescriptor.toString());
		FileOutputStream outputStream = null;
		try {
			outputStream = new FileOutputStream(path);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		try {
			image.compress(Bitmap.CompressFormat.JPEG, 90, outputStream);
			image = null;
		} catch (OutOfMemoryError e) {
			image = null;
			System.gc();
		} finally {
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public Bitmap restoreFromCache(ImageDescriptor imageDescriptor) {
		try {
			InputStream inputStream = new FileInputStream(generateFilePath(imageDescriptor.toString()));
			WeakReference<Bitmap> bitmap = new WeakReference<Bitmap>(BitmapFactory.decodeStream(new FlushedInputStream(inputStream)));
			inputStream.close();
			return bitmap.get();
		} catch (FileNotFoundException e) {
			Log.v(ConstMain.LOG_TAG, "Cached file not found:" + imageDescriptor.toString());
			e.printStackTrace();
			return null;
		} catch (OutOfMemoryError e) {
			Runtime.getRuntime().gc();
			return null;
		} catch (Exception e) {
			return null;
		}
	}

	public WeakReference<Bitmap> restoreFromCacheWeak(ImageDescriptor imageDescriptor) {

		WeakReference<Bitmap> bitmap;
		InputStream inputStream = null;
		try {
			inputStream = new FileInputStream(generateFilePath(imageDescriptor.toString()));
			bitmap = new WeakReference<Bitmap>(BitmapFactory.decodeStream(new FlushedInputStream(inputStream)));
			inputStream.close();
			return bitmap;
		} catch (FileNotFoundException e) {
			Log.v(ConstMain.LOG_TAG, "Cached file not found:" + imageDescriptor.toString());
			e.printStackTrace();
			return null;
		} catch (OutOfMemoryError e) {
			bitmap = null;
			Runtime.getRuntime().gc();
			return null;
		} catch (Exception e) {
			return null;
		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	public boolean isImageCached(ImageDescriptor image) {
		final File file = new File(generateFilePath(image.toString()));
		return file.exists();
	}
}
